<template>
  <div class="card">
    <div class="card-header" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filterFrom">
        <el-form-item>
          审核状态：
          <el-select v-model="filterFrom.verify_status">
            <el-option label="全部" value=""></el-option>
            <el-option label="正在审核" value="0"></el-option>
            <el-option label="通过审核" value="1"></el-option>
            <el-option label="拒绝审核" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          发布日期范围：
          <el-date-picker type="daterange" range-separator="至" v-model="selectDate" placeholder="选择日期范围" @change="handleDateChange"
            :picker-options="datePickerOption" :editable="false"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFilter">确定</el-button>
        </el-form-item>
        <!--<div class="pull-right">
          <el-button type="primary" icon="plus" @click="handleAdd">发布活动</el-button>
        </div>-->
      </el-form>
    </div>
    <div class="card-body card-padding">
      <el-table :data="tableData" style="width: 100%" v-loading="isLoading" border>
        <el-table-column type="expand">
          <template scope="props">
            <img :src="props.row.title_image" style="float: left;max-width:100px;height:75px;" />
            <div style="float: left;margin-left: 10px">
              <dl class="dl-horizontal">
                <dt>发布者ID：</dt>
                <dd>{{props.row.publisher_id}}</dd>
              </dl>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID" width="70"></el-table-column>
        <el-table-column prop="title" label="名称" width="140"></el-table-column>
        <el-table-column prop="start_time" label="活动时间" width="180"></el-table-column>
        <el-table-column prop="address" label="地点" width="200"></el-table-column>
        <el-table-column prop="price" label="价格(￥)" width="100"></el-table-column>
        <el-table-column prop="entry_limit" label="最多报名人数" width="130"></el-table-column>
        <el-table-column prop="entry_count" label="已报名人数" width="130"></el-table-column>
        <el-table-column prop="verify_status" :formatter="statusFormatter" label="审核状态" width="110"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button v-if="scope.row.verify_status===1" size="small" type="warning" @click="handleAction('noPass',scope.row.id)">拒绝审核</el-button>
            <el-button v-else size="small" type="success" @click="handleAction('pass',scope.row.id)">通过审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-pagination">
        <div style="float: left;margin-left: 20px;">
          <el-button size="small" type="text" @click="fetchData"><i class="zmdi zmdi-refresh" />&nbsp;&nbsp;重新加载</el-button>
        </div>
        <el-pagination :current-page="filterFrom.page_number" :page-sizes="[5, 10, 15, 20]" :page-size="filterFrom.page_size" layout="sizes, prev, pager, next, jumper,total"
          :total="totalSize" @size-change="handleSizeChange" @current-change="handlePageChange">
          </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import API from '../../api'
  import mixin from './activity-mixin'

  var filterFrom = {
    verify_status: '',
    start_time: '',
    end_time: '',
    page_number: 1,
    page_size: 10
  }

  export default {
    mixins: [mixin],
    data: function () {
      return {
        typeList: [],
        initTypeList: true,
        isLoading: false,
        tableData: [],
        totalSize: 0,
        filterFrom,
        datePickerOption: {
          disabledDate: function (time) {
            return time.getTime() > Date.now()
          }
        },
        selectDate: ''
      }
    },
    created: function () {
      this.fetchData()
    },
    methods: {
      handleSizeChange: function (val) {
        this.filterFrom.page_size = val
        this.fetchData()
      },
      handlePageChange: function (val) {
        this.filterFrom.page_number = val
        this.fetchData()
      },
      handleDateChange: function (val) {
        if (val) {
          var dates = val.split('至')
          this.filterFrom.start_time = dates[0]
          this.filterFrom.end_time = dates[1]
        } else {
          this.filterFrom.start_time = ''
          this.filterFrom.end_time = ''
        }
      },
      submitFilter: function () {
        if (!this.selectDate) {
          this.filterFrom.start_time = ''
          this.filterFrom.end_time = ''
        }
        this.fetchData()
      },
      handleAdd: function () {
        this.$router.push('/activity/list/edit/0')
      },
      // 编辑
      handleView: function (SelectData) {
        this.$router.push('/activity/list/info/' + SelectData.id)
      },
      handleAction: function (action, selectId) {
        let params = {
          id: selectId
        }
        switch (action) {
          case 'pass': params.verify_status = 1; break
          case 'noPass': params.verify_status = 2; break
          default: break
        }
        API.activityVerify(params,
          (data) => {
            if (!data) return
            this.fetchData()
          }
        )
      },
      fetchData: function () {
        this.isLoading = true
        API.activityList(this.filterFrom,
          (data) => {
            this.isLoading = false

            if (!data) return
            this.tableData = data.list
            this.totalSize = data.total_size
          }
        )
      },
      statusFormatter: function (row) {
        return this.verifyToString(row.verify_status)
      }
    }
  }

</script>

<style>
  .table-pagination {
    text-align: right;
    padding: 10px 0px;
    background: #eef1f6;
    border: 1px solid #dfe6ec;
    border-top: none
  }
</style>
